//
//  ShareList.swift
//  Share
//
//  Created by MBA on 2020/10/4.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI
import Alamofire
import UGSwiftbox

struct ShareList: View {
    
    @ObservedObject var sharesStore =  ShareListStore()
    @State var storeid:Int?
    @State var isNavigation = false
    
    var body: some View {
       
        NavigationView{
            VStack {
                NavigationLink(destination: ShareDetail(shareStore: ShareDetailStore(id: storeid)), isActive: $isNavigation ) {
                    EmptyView()
                   
                }

                let columns: [GridItem] =
                    Array(repeating: .init(.flexible()), count: 2)
                ScrollView {
                    LazyVGrid(columns: columns) {
                        ForEach(sharesStore.shares) { item in
                            
                            ShareCell(shareStore: ShareDetailStore(id: item.id))
                                .onTapGesture(count: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/, perform: {
                                    storeid = item.id
                                    self.isNavigation = true
                                })
                            
                        }
                    }.font(.largeTitle)
                    .padding(.horizontal)
                }
                if sharesStore.loading == true{
                    LoadingView()
                        .background(Color.black.opacity(0.3))
                        .edgesIgnoringSafeArea(.all)
                }
                
            }
            .onAppear(perform: {
                sharesStore.update()
                    
            })
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarTitle(Text("首页"))
            .navigationBarItems(
                trailing: NavigationLink(destination: ShareEdit(shareStore: ShareDetailStore(id: nil))) {
                    Image(systemName: "plus.circle")
                        .frame(width: 32, height: 32, alignment: .center)
                }.buttonStyle(PlainButtonStyle())
                
            )
            
        }
    }
}

struct ShareList_Previews: PreviewProvider {
    static var previews: some View {
        ShareList()
            .preferredColorScheme(.light)
            .previewLayout(.device)
    }
}
